//
//  Convert.m
//  Utility
//
//  Created by Edward Knutson on 10/2/09.
//  Copyright 2009 Six Four Systems. All rights reserved.
//

#import "Convert.h"


@implementation Convert

+ (NSNumber *)floatFromString:(NSString *)string {
	if(string == nil)
		return nil;
	NSScanner *scanner;
	float valueToGet;
	scanner = [NSScanner scannerWithString:string];
	if([scanner scanFloat:&valueToGet] == YES)
		return [NSNumber numberWithFloat:valueToGet];
	return nil;
}

+ (NSNumber *)longLongFromString:(NSString *)string {
	if(string == nil)
		return nil;
	NSScanner *scanner;
	long long valueToGet;
	scanner = [NSScanner scannerWithString:string];
	if([scanner scanLongLong:&valueToGet] == YES)
		return [NSNumber numberWithLongLong:valueToGet];
	return nil;
}

+ (NSDate *)dateFromString:(NSString *)string {
	if(string == nil)
		return nil;
	NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"/Date()"];
	NSString *tsString = [string stringByTrimmingCharactersInSet:charSet];
	if(tsString == nil)
		return nil;
	NSNumber *tsNum = [Convert longLongFromString:tsString];
	if(tsNum == nil)
		return nil;
	return [NSDate dateWithTimeIntervalSince1970:[tsNum doubleValue] / 1000.0f];
}

@end
